﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Categories
{
    public class Vara_Data_SubCategory_Controller : Vara_Data_ISubCategory_Actions
    {
        private static List<Vara_Data_ISubCategory> _subCategories;
        public List<Vara_Data_ISubCategory> SubCategories
        {
            get
            {
                if (_subCategories == null)
                    Refresh_SubCategory_List();

                return _subCategories;
            }
        }
        public List<Vara_Data_ISubCategory> ReturnSubCategoriesByParent(Vara_Data_ICategory ParentCategory)
        {
            return SubCategories.FindAll(delegate(Vara_Data_ISubCategory SubCategory) { return SubCategory.CategoryID == ParentCategory.ID; });
        }
        public Vara_Data_ISubCategory ReturnSubCategory(int ID)
        {
            Vara_Data_ISubCategory SubCategoryToReturn;
            SubCategoryToReturn = SubCategories.Find(delegate(Vara_Data_ISubCategory SubCategory) { return SubCategory.ID == ID; });
            return SubCategoryToReturn;
        }
        public void Refresh_SubCategory_List()
        {
            DataTable DataTableSubCategories;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableSubCategories = NewSqlConnector.ExecuteProcedure("Return_SubCategories");
            }
            List<Vara_Data_ISubCategory> ListOfSubCategories = new List<Vara_Data_ISubCategory>();
            foreach (DataRow R in DataTableSubCategories.Rows)
            {
                Vara_Data_ISubCategory SubCategoryToAppend = new Vara_Data_SubCategory(int.Parse(R["ID"].ToString()));
                SubCategoryToAppend.Name = R["Name"].ToString();
                SubCategoryToAppend.CategoryID = int.Parse(R["ParentCategoryID"].ToString());
                ListOfSubCategories.Add(SubCategoryToAppend);
            }
            _subCategories = ListOfSubCategories;
        }
        public DataTable ReturnAllSubCategoriesByAsset(int AssetID)
        {
                DataTable DataTableSubCategories;
                using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
                { 
                    DataTableSubCategories = NewSqlEngine.ExecuteProcedure("ReturnCategoriesByAsset",new SqlEngineProcedureVariable("@AssetID",AssetID,SqlDbType.Int));
                }
                return DataTableSubCategories;
        }
        public void DeleteAssetSubCategory(int AssetSubCategoryID)
        {
            if (AssetSubCategoryID > 0)
            {
                using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
                {
                    NewSqlEngine.ExecuteProcedure("DeleteAssetSubCategory", new SqlEngineProcedureVariable("@AssetSubCategoryID", AssetSubCategoryID, SqlDbType.Int));
                }
            }
        }
        public void InsertAssetSubCategory(int AssetID, int SubCategoryID)
        {
            if (AssetID > 0)
            {
                using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
                {
                    SqlEngineProcedureVariable[] var = {
                                                            new SqlEngineProcedureVariable("@AssetID", AssetID, SqlDbType.Int),
                                                            new SqlEngineProcedureVariable("@SubCategoryID", SubCategoryID, SqlDbType.Int)
                                                       };
                    NewSqlEngine.ExecuteProcedure("InsertAssetSubCategory", var);
                }
            }
        }
    }
}